import { defineStore } from "pinia"
import { ref } from "vue"

/**
 * @typedef {Object} productModel - 商品对象信息
 * @property {string} _id - 商品id
 * @property {string} name - 商品名字
 * @property {string} cover_pic - 商品封面图
 * @property {string} original_price - 商品原价
 * @property {string} buy_price - 商品当前购买价格
 * @property {string} cate_id - 商品所属分类id
 */

export const useActivityProStore = defineStore( 'ActivityPro', ( ) => {
	/**
	 * @type {import('vue').Ref<productModel[]>}
	 */
	const activityList = ref( [ ] )

	/**
	 * 添加商品到活动列表
	 * @param {productModel} obj - 要添加的商品对象
	 * @returns {void}
	 */
	const setActivityList = ( obj ) => {
		activityList.value.push( obj )
	}

	/**
	 * 从活动列表中移除指定商品
	 * @param {string} proId - 要移除的商品ID
	 * @returns {void}
	 */
	const removeActivityPro = ( proId ) => {
		activityList.value = activityList.value.filter( item => item._id !== proId )
	}

	return {
		activityList,
		setActivityList,
		removeActivityPro
	}
}, {
	// 网页端配置
	// persist: true,

	// 小程序端配置
	persist: {
		storage: {
			getItem( key ) {
				return uni.getStorageSync( key )
			},
			setItem( key, value ) {
				uni.setStorageSync( key, value )
			},
		},
	},
} )